home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_ghostscript.idb / usr / freeware / bin / fixmswrd.pl.z / fixmswrd.pl
Perl Script  |  2002-04-08  |  5KB  |  209 lines

  1. #!/usr/bin/perl
  2. # $Id: fixmswrd.pl,v 1.1 2000/03/09 08:40:39 lpd Exp $
  3.  
  4. # $Id: fixmswrd.pl,v 1.1 2000/03/09 08:40:39 lpd Exp $
  5.  
  6. #   (C) 1997 Anthony Shipman
  7. #   This software is provided 'as-is', without any express or implied
  8. #   warranty.  In no event will the authors be held liable for any damages
  9. #   arising from the use of this software.
  10. #   Permission is granted to anyone to use this software for any purpose,
  11. #   including commercial applications, and to alter it and redistribute it
  12. #   freely, subject to the following restrictions:
  13. #   1. The origin of this software must not be misrepresented; you must not
  14. #      claim that you wrote the original software. If you use this software
  15. #      in a product, an acknowledgment in the product documentation would be
  16. #      appreciated but is not required.
  17. #   2. Altered source versions must be plainly marked as such, and must not be
  18. #      misrepresented as being the original software.
  19. #   3. This notice may not be removed or altered from any source distribution.
  20. #   Anthony Shipman    shipmana@acm.org
  21.  
  22. # This program patches the postscript generated by MS Word printer drivers
  23. # so that they work with ghostview 1.5.  The problem is that the document
  24. # structuring conventions are not followed by Word.  The pages are supposed
  25. # to be independent but they depend on a dictionary being opened outside
  26. # of the pages.  The erroneous structure is
  27. #     %%EndSetup
  28. #     NTPSOct95 begin
  29. #     %%Page: 1 1
  30. #     <text>
  31. #     showpage
  32. #     %%Page: 2 2
  33. #     <text>
  34. #     showpage
  35. #     ......
  36. #     %%Trailer
  37. #     ...
  38. #     end
  39. #     %%EOF
  40. # This only works if the all of the structure around the pages is preserved.
  41. # The opening of NTPSOct95 happens outside of any structured section so
  42. # it is never seen by ghostview.  We change the structure to
  43. #     %%EndSetup
  44. #     %%Page: 1 1
  45. #     NTPSOct95 begin
  46. #     <text>
  47. #     showpage
  48. #     end
  49. #     %%Page: 2 2
  50. #     NTPSOct95 begin
  51. #     <text>
  52. #     showpage
  53. #     end
  54. #     ......
  55. #     %%Trailer
  56. #     ...
  57. #     %%EOF
  58. # That is the dictionary opening is repeated inside each page.
  59. # We add a comment to the document to mark that it has been converted.
  60. # This has the form 
  61. #    %LOCALGhostviewPatched
  62. #
  63. # Usage:
  64. #    fixmswrd [-v] [file [output-file]]
  65.  
  66. require 'getopts.pl';
  67.  
  68. #=================================================================
  69.  
  70. $program = "fixmswrd";
  71.  
  72. sub usage {
  73.     die "Usage: $program [-v] [file [output-file]]\n";
  74. }
  75.  
  76. #=================================================================
  77.  
  78. &Getopts("v") || &usage;
  79.  
  80. $verbose = $opt_v;
  81.  
  82.  
  83. $infile = shift(@ARGV);
  84. if ($infile)
  85. {
  86.     open(INFILE, $infile) || die "$program: Cannot read from $infile\n";
  87.     $handle = "INFILE";
  88. }
  89. else
  90. {
  91.     $handle = "STDIN";
  92. }
  93.  
  94.  
  95. $outfile = shift(@ARGV);
  96. if ($outfile)
  97. {
  98.     open(OUTFILE, ">$outfile") || die "$program: Cannot write to $outfile\n";
  99.     select(OUTFILE);
  100. }
  101.  
  102. #  This reads the header comments and detects the presence of the marker.
  103. $have_marker = 0;
  104.  
  105. undef $dict_name;
  106. undef $dict_line;
  107.  
  108. &read_comments;
  109. &put_comments;
  110.  
  111. if ($have_marker)
  112. {
  113.     $verbose && print STDERR "$program: Warning - already converted\n";
  114.  
  115.     while(<$handle>)        # pass the file through unchanged.
  116.     {
  117.     print;
  118.     }
  119. }
  120. else
  121. {
  122.     $seen_trailer = 0;
  123.  
  124.     while(<$handle>)        # massage the file
  125.     {
  126.     if ($dict_line)
  127.     {
  128.         next if (/$dict_line/o);        # drop the old begin line
  129.         $seen_trailer = 1 if (/^%%Trailer/);
  130.         next if ($seen_trailer and /^end/);    # drop the old end line
  131.     }
  132.  
  133.     print;
  134.  
  135.     if (/^%%Page:/)
  136.     {
  137.         print "$dict_name begin\n";    # add at the start of the page
  138.     }
  139.     elsif (/^showpage/)
  140.     {
  141.         print "end\n";            # add at the end of the page
  142.     }
  143.     elsif (/^%%BeginResource: procset (\S+)/)
  144.     {
  145.         $dict_name = $1;
  146.         $dict_line = "^$dict_name begin";
  147.     }
  148.     elsif (/^%%BeginProcSet: (\S+)/)    # for older document versions
  149.     {
  150.         $dict_name = $1;
  151.         $dict_line = "^$dict_name begin";
  152.     }
  153.     elsif (/^%%EndProlog:/)
  154.     {
  155.         unless ($dict_line) 
  156.         {
  157.         $verbose && 
  158.             print STDERR "$program: Warning - unrecognised document structure\n";
  159.         }
  160.     }
  161.     }
  162. }
  163.  
  164. exit 0;
  165.  
  166. #=================================================================
  167.  
  168.  
  169. #  This reads all of the header comments into an array which we can write
  170. #  out again later.  In addition we detect the presence of the marker comment.
  171.  
  172. sub read_comments
  173. {
  174.     @headers = ();
  175.  
  176.     while (<$handle>)
  177.     {                # without chopping
  178.     push(@headers, $_);
  179.     if (/^%LOCALGhostviewPatched/)
  180.     {
  181.         $have_marker = 1;
  182.     }
  183.     last if /^%%EndComments/;
  184.     }
  185. }
  186.  
  187.  
  188.  
  189. sub put_comments 
  190. {
  191.     foreach $h (@headers)
  192.     {
  193.     if (!$have_marker and ($h =~ /^%%EndComments/))
  194.     {
  195.         print "%LOCALGhostviewPatched\n";
  196.     }
  197.     print $h;        # contains the newline
  198.     }
  199. }
  200.